home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cnews014.zip / PAINTSRC.ZIP / PAINT.C
C/C++ Source or Header  |  1989-02-20  |  34KB  |  1,302 lines

  1. /* PAINT.C
  2.  *
  3.  * Scott R. Houck
  4.  * Written in Turbo C 2.0
  5.  *
  6.  * Add the EGAVGA.BGI driver to graphics.lib using the BGIOBJ program.
  7.  *
  8.  * Compile with:  tcc -ml paint graphics.lib
  9.  *
  10.  * This is a simple paint program
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <dos.h>
  16. #include <mem.h>
  17. #include <alloc.h>
  18. #include <conio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <io.h>
  22. #include <ctype.h>
  23. #include <graphics.h>
  24.  
  25.  
  26. /* Mouse buttons */
  27.  
  28. #define LEFTBUTTON   1
  29. #define RIGHTBUTTON  2
  30.  
  31. /* Clipping */
  32.  
  33. #define CLIPPING_ON  1
  34. #define CLIPPING_OFF 0
  35.  
  36. /* Boolean values */
  37.  
  38. #define TRUE   1
  39. #define FALSE  0
  40.  
  41. /* Dialog box options */
  42.  
  43. #define YES    1
  44. #define NO     0
  45.  
  46. /* Menu choices */
  47.  
  48. #define IGNORE             -1
  49. #define PAINT              0
  50. #define FILL               1
  51. #define ERASER             2
  52. #define CLEAR              3
  53. #define SAVE               4
  54. #define LOAD               5
  55. #define QUIT               6
  56. #define RUBBER_LINE        7
  57. #define RUBBER_RECTANGLE   8
  58.  
  59.  
  60. #define LINE         0
  61. #define RECTANGLE    1
  62.  
  63. /* Some useful typedefs */
  64.  
  65. typedef struct pointtype   POINT;         /* A point with x and y as ints */
  66. typedef POINT              EXTENT [2];    /* Rectangular extent */
  67.  
  68.  
  69. /* Function prototypes */
  70.  
  71. void    InitializeGraphics(void);
  72. void    DrawScreen(void);
  73. int     MouseReset(int *);
  74. void    MouseOn(void);
  75. void    MouseOff(void);
  76. int     MouseStatus(POINT *);
  77. void    MouseWaitForPress(int, POINT *);
  78. void    MouseWaitForRelease(int, POINT *);
  79. void    MouseSetCursor(int [16][2]);
  80. void    HighlightMenu(int, int, int);
  81. int     PointInExtent(POINT, EXTENT);
  82. void    HandleDraw(POINT);
  83. int     PickCorrInMenu(POINT);
  84. int     PickCorrInColor(POINT);
  85. int     PickCorrInPattern(POINT);
  86. void    HandlePick(POINT);
  87. void    SetForeground(int);
  88. void    SetBackground(int);
  89. void    SetPattern(int);
  90. void    DoPaint(POINT);
  91. void    DoFill(POINT);
  92. void    DoEraser(POINT);
  93. void    DoClear(void);
  94. void    DoSave(void);
  95. void    DoLoad(void);
  96. void    DoRubber(int, POINT);
  97. int     DialogBox(void);
  98. void    Beep(void);
  99. void    LowBeep(void);
  100. int     GetChar(char *, char *, int *);
  101. char *  GetString(char *, int, int, int, char *, char *, char *, int,
  102.             int (*)(int, int));
  103. int     PromptForFilename(char *);
  104.  
  105.  
  106. /* Global variables */
  107.  
  108. int maxx, maxy;                        /* Maximum pixel values */
  109. int dminx, dminy, dmaxx, dmaxy;        /* Drawing area coordinates */
  110. int bgminx, bgminy, bgmaxx, bgmaxy;    /* Background box */
  111. int fgminx, fgminy, fgmaxx, fgmaxy;    /* Foreground box */
  112. int rl1x, rl1y, rl2x, rl2y;            /* Rubber line coordinates */
  113. int rr1x, rr1y, rr2x, rr2y;            /* Rubber rectangle coordinates */
  114. int xminx, xminy, xmaxx, xmaxy;        /* Dialog box coordinates */
  115. EXTENT drawExtent;                     /* Extent of the drawing area */
  116. EXTENT colorExtent[16];                /* Extents of the 16 color boxes */
  117. EXTENT patternExtent[11];              /* Extents of the 11 patterns */
  118. EXTENT yesExtent, noExtent;            /* Extent of the YES/NO buttons */
  119. int currentColor;                      /* Current foreground color */
  120. int fillPattern;                       /* Current fill pattern */
  121. int fillColor;                         /* Current fill color */
  122. unsigned dialogSize;                   /* Dialog box image size */
  123. unsigned drawSize;                     /* Drawing size */
  124. unsigned promptSize;                   /* Size of prompt box */
  125. void *dialogBuffer, *saveBuffer;       /* Dialog box and screen buffers */
  126. void *promptBuffer, *drawBuffer;       /* Prompt box and drawing buffers */
  127. int fminx, fminy, fmaxx, fmaxy;        /* File prompt box coords */
  128. int promptx, prompty;                  /* File prompt x and y coords */
  129.  
  130. struct {
  131.    EXTENT extent;
  132.    char *text;
  133. } menuBox[10];       /* menu buttons */
  134.  
  135. int mode;            /* current mode */
  136.  
  137.  
  138. /* The following two arrays define the eraser-type cursor and the
  139.  * regular arrow cursor for use in the MouseSetCursor() routine.
  140.  */
  141.  
  142. int eraser [16][2] = {
  143.    { 0xFFFF, 0xFFFF },  /* screen mask */
  144.    { 0xFFFF, 0xFFFF },
  145.    { 0xFFFF, 0xFFFF },
  146.    { 0xFFFF, 0xFFFF },
  147.    { 0xFFFF, 0xFFFF },
  148.    { 0xFFFF, 0xFFFF },
  149.    { 0xFFFF, 0xFFFF },
  150.    { 0xFFFF, 0xFFFF },
  151.  
  152.    { 0xFFFF, 0x8001 },  /* cursor mask */
  153.    { 0x8001, 0x8001 },
  154.    { 0x8001, 0x8001 },
  155.    { 0x8001, 0xFFFF },
  156.    { 0x0000, 0x0000 },
  157.    { 0x0000, 0x0000 },
  158.    { 0x0000, 0x0000 },
  159.    { 0x0000, 0x0000 }
  160. };
  161.  
  162. int cursor [16][2] = {
  163.    { 0x3FFF, 0x1FFF },  /* screen mask */
  164.    { 0x0FFF, 0x07FF },
  165.    { 0x03FF, 0x01FF },
  166.    { 0x00FF, 0x007F },
  167.    { 0x003F, 0x001F },
  168.    { 0x01FF, 0x10FF },
  169.    { 0x30FF, 0xF87F },
  170.    { 0xF87F, 0xFC7F },
  171.  
  172.    { 0x0000, 0x4000 },  /* cursor mask */
  173.    { 0x6000, 0x7000 },
  174.    { 0x7800, 0x7C00 },
  175.    { 0x7E00, 0x7F00 },
  176.    { 0x7F80, 0x7C00 },
  177.    { 0x6C00, 0x4600 },
  178.    { 0x0600, 0x0300 },
  179.    { 0x0300, 0x0000 }
  180. };
  181.  
  182.  
  183.  
  184. main()
  185. {
  186.    POINT position;
  187.    int buttonPressed;
  188.  
  189.    InitializeGraphics();
  190.    DrawScreen();
  191.    MouseOn();
  192.  
  193.    HighlightMenu(mode = PAINT, LIGHTGRAY, BLACK);
  194.  
  195.    while (mode != QUIT)
  196.       {
  197.       do
  198.          {
  199.          buttonPressed = MouseStatus(&position);
  200.          if (mode == ERASER && PointInExtent(position, drawExtent))
  201.             MouseSetCursor(eraser);
  202.          else
  203.             MouseSetCursor(cursor);
  204.          }
  205.       while (!(buttonPressed & (RIGHTBUTTON | LEFTBUTTON)));
  206.  
  207.       if (PointInExtent(position, drawExtent))
  208.          HandleDraw(position);
  209.       else
  210.          HandlePick(position);
  211.       }
  212.  
  213.    MouseOff();
  214.    closegraph();
  215. }
  216.  
  217.  
  218. /* InitializeGraphics() initializes the graphics package and mouse driver.
  219.  * If there is an error, the program aborts with an appropriate error
  220.  * message.
  221.  */
  222. void InitializeGraphics()
  223. {
  224.    int graphDriver, graphMode, errorCode;
  225.    int numberOfButtons;
  226.  
  227.    if (MouseReset(&numberOfButtons) == 0)
  228.       {
  229.       printf("Mouse driver is not installed\n");
  230.       exit(1);
  231.       }
  232.  
  233.    if (numberOfButtons < 2)
  234.       {
  235.       printf("This program requires a mouse with at least two buttons\n");
  236.       exit(2);
  237.       }
  238.  
  239.    if (registerbgidriver(EGAVGA_driver) < 0)
  240.       exit(1);
  241.  
  242.    graphDriver = DETECT;
  243.    initgraph(&graphDriver, &graphMode, "");
  244.    errorCode = graphresult();
  245.    if (errorCode != grOk)
  246.       {
  247.       printf("Graphics System Error: %s\n", grapherrormsg(errorCode));
  248.       exit(1);
  249.       }
  250.  
  251.    /* Get maximum x and y screen coordinates */
  252.  
  253.    maxx = getmaxx();
  254.    maxy = getmaxy();
  255. }
  256.  
  257.  
  258. void DrawScreen()
  259. {
  260.    int i;
  261.    int cminx, cminy, cmaxx, cmaxy;  /* color box coordinates */
  262.    int pminx, pminy, pmaxx, pmaxy;  /* pattern box coordinates */
  263.    int cwidth;                      /* width of color box */
  264.    int cheight;                     /* height of color box */
  265.    int pwidth;                      /* width of pattern box */
  266.    int gap;                         /* gap between various objects */
  267.    int bminx, bminy, bmaxx, bmaxy;        /* Menu buttons */
  268.    int bwidth, bheight, bgap, bstart;     /* Menu buttons */
  269.    int dwidth, dheight;                   /* Drawing area dimensions */
  270.    int xwidth, xheight;                   /* Dialog box dimensions */
  271.    int yminx, yminy, ymaxx, ymaxy;        /* YES button */
  272.    int nminx, nminy, nmaxx, nmaxy;        /* NO button */
  273.    int fwidth, fheight;                   /* Height of file prompt box */
  274.    static char *boxText[] = { "PAINT", "FILL", "ERASER", "CLEAR", "SAVE",
  275.       "LOAD", "QUIT", "", "" };
  276.  
  277.    setvisualpage(0);
  278.    setactivepage(0);
  279.  
  280.    /* Draw the main background */
  281.  
  282.    setcolor(WHITE);
  283.    rectangle(0, 0, maxx, maxy);
  284.    setfillstyle(INTERLEAVE_FILL, CYAN);
  285.    floodfill(1, 1, WHITE);
  286.  
  287.    /* Draw the drawing area */
  288.  
  289.    drawExtent[0].x = dminx = (int)(0.30 * maxx);
  290.    drawExtent[0].y = dminy = (int)(0.05 * maxy);
  291.    drawExtent[1].x = dmaxx = (int)(0.95 * maxx);
  292.    drawExtent[1].y = dmaxy = (int)(0.80 * maxy);
  293.  
  294.    drawSize = imagesize(dminx, dminy, dmaxx, dmaxy);
  295.    drawBuffer = malloc(drawSize);
  296.  
  297.    dwidth